<template>
  <div class="ai-analysis-page">
    <div class="ai-analysis-header">
      <h1>AI学情分析</h1>
      <p>智能分析你的学习情况，提供个性化学习建议和数据可视化。</p>
    </div>
    <div class="ai-analysis-content">
      <div class="chat-container">
        <div class="chat-messages" ref="chatMessages">
          <div v-for="(msg, index) in messages" :key="index" :class="['message', msg.type]">
            <div class="message-content">{{ msg.content }}</div>
          </div>
        </div>
        <div class="chat-input">
          <el-input v-model="userInput" placeholder="请输入您的问题..." @keyup.enter="sendMessage" />
          <el-button type="primary" @click="sendMessage" :loading="loading">发送</el-button>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted, nextTick } from 'vue';
import axios from 'axios';

const messages = ref([]);
const userInput = ref('');
const loading = ref(false);
const chatMessages = ref(null);

const sendMessage = async () => {
  if (!userInput.value.trim()) return;
  const content = userInput.value;
  messages.value.push({ type: 'user', content });
  userInput.value = '';
  loading.value = true;
  try {
    const response = await axios.post('/ai/chat', content);
    messages.value.push({ type: 'ai', content: response.data });
  } catch (error) {
    console.error('Error:', error);
    messages.value.push({ type: 'ai', content: '抱歉，发生了错误，请稍后再试。' });
  } finally {
    loading.value = false;
    await nextTick();
    if (chatMessages.value) {
      chatMessages.value.scrollTop = chatMessages.value.scrollHeight;
    }
  }
};

onMounted(() => {
  messages.value.push({ type: 'ai', content: '你好！我是编程小智，请你把生成的学习报告发给我，我将为你分析' });
});
</script>

<style scoped>
.ai-analysis-page {
  max-width: 900px;
  margin: 40px auto;
  background: #fff;
  border-radius: 18px;
  box-shadow: 0 4px 24px rgba(50,197,255,0.08);
  padding: 48px 36px 36px 36px;
}
.ai-analysis-header {
  text-align: center;
  margin-bottom: 32px;
}
.ai-analysis-header h1 {
  font-size: 2.2rem;
  font-weight: 800;
  color: #32C5FF;
  margin-bottom: 10px;
}
.ai-analysis-header p {
  color: #666;
  font-size: 1.15rem;
}
.ai-analysis-content {
  min-height: 300px;
  display: flex;
  align-items: center;
  justify-content: center;
}
.chat-container {
  width: 100%;
  max-width: 600px;
  border: 1px solid #eee;
  border-radius: 14px;
  overflow: hidden;
  background: #f6fafd;
  box-shadow: 0 2px 12px rgba(50,197,255,0.10);
}
.chat-messages {
  height: 400px;
  overflow-y: auto;
  padding: 24px 18px 18px 18px;
  background: #f9fafd;
}
.message {
  margin-bottom: 16px;
  display: flex;
}
.message-content {
  display: inline-block;
  padding: 12px 18px;
  border-radius: 18px;
  font-size: 1.08rem;
  line-height: 1.7;
  box-shadow: 0 2px 8px rgba(50,197,255,0.08);
  min-width: 48px;
  max-width: 70vw;
  max-width: 420px;
  word-break: break-all;
  background: #fff;
  color: #222;
  transition: background 0.2s;
}
.message.user {
  justify-content: flex-end;
}
.message.user .message-content {
  background: #e1f5fe;
  color: #1976d2;
  border-bottom-right-radius: 6px;
  border-top-right-radius: 18px;
  border-top-left-radius: 18px;
  border-bottom-left-radius: 18px;
}
.message.ai {
  justify-content: flex-start;
}
.message.ai .message-content {
  background: #f1f1f1;
  color: #444;
  border-bottom-left-radius: 6px;
  border-top-right-radius: 18px;
  border-top-left-radius: 18px;
  border-bottom-right-radius: 18px;
}
.chat-input {
  display: flex;
  padding: 18px;
  background: #fff;
  border-top: 1px solid #eee;
}
.chat-input .el-input {
  flex: 1;
  margin-right: 10px;
}
.chat-input .el-button {
  min-width: 80px;
  font-size: 16px;
  border-radius: 8px;
}
</style>
